#pragma once
#include <bits/stdint-uintn.h>
#include <memory>
#include <string>
#include <cstddef>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <odb/nullable.hxx>
#include <odb/core.hxx>
#include "chat_session_member.hxx"


namespace entity {

enum ChatSessionType {
    SINGLE = 1,
    GROUP
};

#pragma db object table("chat_session")
class ChatSession
{
public:
    ChatSession() {}
    ChatSession(const std::string& sessionId, const std::string& sessionName, const uint8_t type)
        : sessionId_(sessionId), sessionName_(sessionName), sessionType_(type) {}

    void setSessionId(const std::string& val) { sessionId_ = val; }
    std::string sessionId() const { return sessionId_; }

    void setSessionName(const std::string& val) { sessionName_ = val; }
    std::string sessionName() const { return sessionName_; }

    void setSessionType(const uint8_t val) { sessionType_ = val; }
    uint8_t sessionType() const { return sessionType_; }
    
private:
    friend class odb::access; 
    
    #pragma db id auto
    uint32_t id_;

    #pragma db column("session_id") type("varchar(32)") index unique
    std::string sessionId_;

    #pragma db column("session_name") type("varchar(32)") index
    std::string sessionName_;

    #pragma db column("session_type")
    uint8_t sessionType_;    // 1-单聊  2-群聊
};

// // 这里条件必须是指定条件: 
// css::chat_session_type==1 && csm1.user_id=uid && csm2.user_id != csm1.user_id
// 连接 ChatSessionMember 两次是为了将这个单聊会话的两个用户数据拿到
#pragma db view object(ChatSession = css) \
                object(ChatSessionMember = csm1 : css::sessionId_ == csm1::sessionId_) \
                object(ChatSessionMember = csm2 : css::sessionId_ == csm2::sessionId_) \
                query((?))
struct SingleChatSession
{
    #pragma db column(css::sessionId_)
    std::string chatSessionId;
    #pragma db column(csm2::userId_)
    std::string friendId;
};

#pragma db view object(ChatSession = css) \
                object(ChatSessionMember = csm : css::sessionId_ == csm::sessionId_) \
                query((?))
struct GroupChatSession
{
    #pragma db column(css::sessionId_)
    std::string chatSessionId;
    #pragma db column(css::sessionName_)
    std::string chatSessionName;
};

using ChatSessionPtr = std::shared_ptr<ChatSession>;

}
// odb -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time chat_session.hxx
